/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uq.comp7705.util;

import edu.uq.comp7705.entity.Person;
import edu.uq.comp7705.entity.Session;
import edu.uq.comp7705.entity.SessionDrink;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author javierpanchi
 */
public class Util {

    public static double calculateBAC(double mg, double hours, int packs, double weight, boolean isMan) {
        double r = 0;
        if (isMan) {
            r = 0.73;
        } else {
            r = 0.66;
        }
        double temp = (packs * mg) / (r * weight) / 10;
        return Math.abs(temp - (15 * hours)) / 100;
    }
    
    
    public static List<String> calculateRealTimeBAC(List<Session> sessions){
        List<String> peopleDrunk=new ArrayList();
        UtilDB utilDB=new UtilDB();
        for(Session session:sessions){
            double acLevel=0;
            Integer acQuantity=0;
            double weight=80;
            String gender="F"; 
            for(SessionDrink sessionDrink:(List<SessionDrink>)utilDB.getListSessionDrinkBySessionId(session.getId())){
                acLevel+=sessionDrink.getDrkDrinksId().getLevel();
                acQuantity+=sessionDrink.getQuantity();
            }
            Person person=utilDB.getPersonByFacebookId(session.getFacebookid());
            if(person!=null){
                weight=person.getWeight();
                gender=person.getGender();
            }
            double bac=calculateBAC(acLevel,Util.getTotalTime(session), acQuantity, weight,gender.equalsIgnoreCase("M"));
            if(bac>0.5){
                peopleDrunk.add(session.getNameFacebook());
            }
        }
        return peopleDrunk;
    }

    public static void updateSession(HttpServletRequest request) {
        List<Session> sessionDrink = (List<Session>) request.getSession().getAttribute("sessionDrink");
        List<Session> newSessionListDrink = new ArrayList<Session>();
        UtilDB utilDB = new UtilDB();
        for (Session session : sessionDrink) {
            Session newSession = utilDB.getSessionById(session.getId());
            newSession.setSessionDrinkList(utilDB.getListSessionDrinkBySessionId(newSession.getId()));
            newSessionListDrink.add(newSession);
        }
        request.getSession().removeAttribute("sessionDrink");
        request.getSession().setAttribute("sessionDrink", newSessionListDrink);
    }

    public static double getTotalAlcoholLevel(Session session) {
        //Control por el tiempo
        double total = 0;
        for (SessionDrink drinkSession : session.getSessionDrinkList()) {
            total += drinkSession.getDrkDrinksId().getLevel();
        }
        return total;
    }

    public static Integer getTotalDrinks(Session session) {
        //Control por el tiempo
        Integer total = 0;
        for (SessionDrink drinkSession : session.getSessionDrinkList()) {
            total += 1;
        }
        return total;
    }

    public static double getTotalTime(Session session) {
        //Control por el tiempo
        double diff = (new Date()).getTime() - session.getSince().getTime();
        double value = diff / (1000 * 60);
        return value;
    }
    public static Session getSessionByFacebookId(List<Session>sessions,String facebookId){
        for(Session session:sessions){
            if(session.getFacebookid().equals(facebookId)){
                return session;
            }
        }
        return null;
    }
}
